/**
 * Design and form layout by Dagny Jackson.
 * CardLayout coding done by Lidiya.
 * Form handlers for opening and closing children frames by Jon Axford.
 */

package Forms;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.text.html.*;
import javax.swing.WindowConstants;
import teComponents.teJButton;
import teComponents.teJFrame;
import teComponents.teJinternalFrame;

import javax.swing.SwingUtilities;

import Classes.Agents;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class MainMDIForm extends teComponents.teJFrame 
{

	/**
	 * function to create an imageicon created by Dagny Jackson
	 * to use a custom graphic (later as a logo in a label)
	 * Returns an ImageIcon, or null if the path was invalid.
	 */ 
	protected ImageIcon createAnImageIcon(String path, String description) {
	    java.net.URL imgURL = getClass().getResource(path);
	    if (imgURL != null) {
	        return new ImageIcon(imgURL, description);
	    } else {
	        System.err.println("The following file was not found: " + path);
	        return null;
	    }
	}
	
	
	//initialize a string for access privileges with nothing,
	//which will signal to the cardlayout to display
	//the login sidebar. If someone logs in who is a senior agent
	//they are considered 'manager'. Change this value to 'manager'.
	//If they are junior or intermediate agents, then change to 'employee'.
	//Depending on if the string is manager or employee, a different card will
	//be displayed in the cardlayout.
	
	private String cardString = "";
	//declare the CardHolder panel, which will house the 'cards' of the CardLayout
	private JPanel pnlCardHolder;
	private JPanel pnlSideBar;
	private JLabel lblLogo;
	private teJFrame mySelf;

	//declare the three card names:
	final static String loginCard = "loginCard";
	final static String managerCard = "managerCard";
	final static String employeeCard = "employeeCard";
	
	//the three panels that will contain the cards above:
	 private JPanel loginPanel;
	 private JPanel managerPanel;
	 private JPanel employeePanel;
	
	//declare the MDI frame that will house the other windows:
	private JDesktopPane dsktpPaneMain;
	
	//declare the image logo
	private BufferedImage teLogo;
	
	//declare the buttons
	 private teJButton btnAgencies;
	 private teJButton btnProdsSupps;
	 private teJButton btnSuppliers;
	 private teJButton btnProducts;
	 private teJButton btnPackages;
	 private teJButton btnLogin;
	 private teJButton btnLgout;
	 private teJButton btnCustomers;
	 private teJButton btnAgents;
	 
	
	private JScrollPane jScrollPane1;
	
	
	private LoginForm  logIn;

	/*public LoginForm getLogIn() 
	{
		if(logIn == null)
		{
			logIn = new  LoginForm(this);
		}
		return logIn;
	}*/

	public void setCardStr(String str)
	{
		if (str == null)
		{
			str = loginCard;
		}
		cardString = str; 
		CardLayout cl = (CardLayout)(pnlCardHolder.getLayout());
		 if(cl != null)
	       cl.show(pnlCardHolder, cardString);
		 else
		 {
			 System.out.println("cl = null"); 	 
		 } 
		System.out.println(cardString);
	}

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainMDIForm inst = new MainMDIForm();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public MainMDIForm() {
		super();
		myWindows = new ArrayList<teJinternalFrame>();
		initGUI();
		mySelf=this;
	}
	private JPanel createLogInPanel()
	{
		
		JPanel loginPanel = new JPanel();
	    loginPanel.setBounds(12, 154, 150, 329);
	    loginPanel.setBackground(new java.awt.Color(251,253,242));
	
		btnLogin = new teJButton();
		loginPanel.add(btnLogin);
		btnLogin.setText("Login");
		btnLogin.setToolTipText("Login to the Travel Experts Desktop Application");
		btnLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("LogIn press");	 
				dsktpPaneMain.add(new LoginForm(mySelf));
			}
		});
	
		return loginPanel;
	}
	
	private void createButtons()
	{
		btnCustomers = new teJButton();
		btnCustomers.setText("<html><center>Maintain <br/> Customers</center></html>");
		
		btnPackages = new teJButton();
		btnPackages.setText("<html><center>Maintain <br/> Packages</center></html>");
		
		btnProducts = new teJButton();
		btnProducts.setText("<html><center>Maintain <br/> Products</center></html>");
		
		btnSuppliers = new teJButton();
		btnSuppliers.setText("<html><center>Maintain <br/> Suppliers</center></html>");
		
		btnAgents = new teJButton();
		btnAgents.setText("<html><center>Maintain <br/>Agents & <br/> Agencies</center></html>");
		
		btnProdsSupps = new teJButton(); 
		btnProdsSupps.setText("<html><center>Maintain <br/>Products & <br/> Suppliers</center></html>");
		
		btnLgout = new teJButton();
		btnLgout.setText("LogOut");
		btnLgout.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("LogOut press");	 
				setCardStr(loginCard);
				killAll();
			}
		});
	}
	private JPanel createManagerPanel()
	{
		 JPanel managerPanel = new JPanel();
	     managerPanel.setBounds(12, 154, 150, 329);
	     managerPanel.setBackground(new java.awt.Color(251,253,242));	
	
	    btnCustomers = new teJButton();
		btnCustomers.setText("<html><center>Maintain <br/> Customers</center></html>");
		managerPanel.add(btnCustomers);
		btnCustomers.setBounds(25, 12, 100, 34);
		btnCustomers.setPreferredSize(new java.awt.Dimension(100, 34));
		btnCustomers.setToolTipText("Opens the window to maintain Customers, including customer payments and bookings");
		btnCustomers.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new CustomerForm());
			}
		});
		
		btnPackages = new teJButton();
		btnPackages.setText("<html><center>Maintain <br/> Packages</center></html>");
		managerPanel.add(btnPackages);
		btnPackages.setBounds(25, 59, 100, 34);
		btnPackages.setPreferredSize(new java.awt.Dimension(100, 34));
		btnPackages.setToolTipText("Opens the window to maintain Packages");
		btnPackages.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new PackagesForm());
			}
		});
		
		btnProducts = new teJButton();
		btnProducts.setText("<html><center>Maintain <br/> Products</center></html>");
		managerPanel.add(btnProducts);
		btnProducts.setBounds(25, 106, 100, 34);
		btnProducts.setPreferredSize(new java.awt.Dimension(100, 34));
		btnProducts.setToolTipText("Opens the window to maintain Products");
		btnProducts.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new ProductsForm());
			}
		});
		
		btnSuppliers = new teJButton();
		btnSuppliers.setText("<html><center>Maintain <br/> Suppliers</center></html>");
		managerPanel.add(btnSuppliers);
		btnSuppliers.setBounds(25, 154, 100, 34);
		btnSuppliers.setPreferredSize(new java.awt.Dimension(100, 34));
		btnSuppliers.setToolTipText("Opens the window to maintain Suppliers");
		btnSuppliers.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new SuppliersForm());
			}
		});
		
		btnAgencies = new teJButton();
		btnAgencies.setText("<html><center>Maintain <br/>Agencies</center></html>");
		managerPanel.add(btnAgencies);
		btnAgencies.setBounds(25, 265, 100, 34);
		btnAgencies.setPreferredSize(new java.awt.Dimension(100, 34));
		btnAgencies.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new AgenciesForm());
			}
		});
		
		btnAgents = new teJButton();
		btnAgents.setText("<html><center>Maintain <br/>Agents</center></html>");
		managerPanel.add(btnAgents);
		btnAgents.setBounds(25, 265, 100, 34);
		btnAgents.setPreferredSize(new java.awt.Dimension(100, 34));
		btnAgents.setToolTipText("Opens the window to maintain Agents");
		btnAgents.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new AgentsForm());
			}
		});
		
		btnProdsSupps = new teJButton(); 
		btnProdsSupps.setText("<html><center>Maintain <br/>Products & <br/> Suppliers</center></html>");
	    managerPanel.add(btnProdsSupps);
		btnProdsSupps.setBounds(25, 201, 100, 60);
		btnProdsSupps.setPreferredSize(new java.awt.Dimension(100, 54));
		btnProdsSupps.setToolTipText("Opens the window to maintain Product and Supplier combinations");
		btnProdsSupps.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dsktpPaneMain.add(new ProductSupplierForm());
			}
		});
		
		btnLgout = new teJButton();
		btnLgout.setText("LogOut");
		managerPanel.add(btnLgout);
		btnLgout.setPreferredSize(new java.awt.Dimension(100, 23));
		btnLgout.setToolTipText("Logout of the Travel Experts Desktop Application");
        btnLgout.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("LogOut press");	 
				setCardStr(loginCard);
				killAll();
			}
		});
		
		
		return managerPanel;
	}
	
	private JPanel createEmployeePanel()
	{
		 JPanel employeePanel = new JPanel();
		 employeePanel.setBounds(12, 154, 150, 329);
		 employeePanel.setBackground(new java.awt.Color(251,253,242));

		 btnCustomers = new teJButton();
			btnCustomers.setText("<html><center>Maintain <br/> Customers</center></html>");
			employeePanel.add(btnCustomers);
			btnCustomers.setBounds(25, 12, 100, 34);
			btnCustomers.setPreferredSize(new java.awt.Dimension(100, 47));
			btnCustomers.setToolTipText("Opens the window to maintain Customers, including customer payments and bookings");
			
		 
		 btnProducts = new teJButton();
		   btnProducts.setText("<html><center>Maintain <br/> Products</center></html>");
		   employeePanel.add(btnProducts);
		   btnProducts.setBounds(25, 129, 100, 44);
		   btnProducts.setToolTipText("Opens the window to maintain Products");
		   btnProducts.setPreferredSize(new java.awt.Dimension(100, 47));

		    btnSuppliers = new teJButton();
			btnSuppliers.setText("<html><center>Maintain <br/> Suppliers</center></html>");
			employeePanel.add(btnSuppliers);
			btnSuppliers.setBounds(25, 188, 100, 44);
			btnSuppliers.setToolTipText("Opens the window to maintain Suppliers");
			btnSuppliers.setPreferredSize(new java.awt.Dimension(100, 47));

			btnProdsSupps = new teJButton(); 
			btnProdsSupps.setText("<html><center>Maintain <br/>Products & <br/> Suppliers</center></html>");
			employeePanel.add(btnProdsSupps);
			btnProdsSupps.setBounds(25, 247, 100, 70);
			btnProdsSupps.setToolTipText("Opens the window to maintain Product and Supplier combinations");
			btnProdsSupps.setPreferredSize(new java.awt.Dimension(100, 69));
			
			btnPackages = new teJButton();
			btnPackages.setText("<html><center>Maintain <br/> Packages</center></html>");
			employeePanel.add(btnPackages);
			btnPackages.setBounds(25, 70, 100, 44);
			btnPackages.setToolTipText("Opens the window to maintain Packages");
			btnPackages.setPreferredSize(new java.awt.Dimension(100, 47));
			
			btnLgout = new teJButton();
			btnLgout.setText("LogOut");
			employeePanel.add(btnLgout);
			btnLgout.setPreferredSize(new java.awt.Dimension(100, 35));
			btnLgout.setToolTipText("Logout of the Travel Experts Desktop Application");
			btnLgout.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					System.out.println("LogOut press");	 
					setCardStr(loginCard);
				}
			});
			
			return employeePanel;
			
	}
	
	private void addComponentsToPane(Container pane)
	{
		
		loginPanel = createLogInPanel();
		managerPanel = createManagerPanel();
		employeePanel = createEmployeePanel();
		
		pnlCardHolder = new JPanel(new CardLayout());
		pnlCardHolder.setBounds(15, 155, 147, 329);
		
		
		pnlCardHolder.add(loginPanel, loginCard);
		loginPanel.setPreferredSize(new java.awt.Dimension(147, 445));
		pnlCardHolder.add(employeePanel, employeeCard);	
		pnlCardHolder.add(managerPanel, managerCard);
		managerPanel.setPreferredSize(new java.awt.Dimension(147, 360));

		pane.add(pnlCardHolder);
		{	
			/**
			 * Label containing Logo created by Dagny Jackson
			 */
			ImageIcon logo = createAnImageIcon("/Images/TELogo_140_solid.png", "Travel Experts Logo");
			
			lblLogo = new JLabel(logo, JLabel.CENTER);
			pnlSideBar.add(lblLogo);
			//lblLogo.setText("jLabel1");
			lblLogo.setBounds(0, 0, 176, 155);
			
		}				
	}
	


	private void initGUI() {
		try {
			this.setTitle("Travel Experts Desktop Application");
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			
			
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(182, 6, 1237, 708);
				
				dsktpPaneMain = new JDesktopPane();
				jScrollPane1.setViewportView(dsktpPaneMain);
				dsktpPaneMain.setBounds(182, 26, 977, 475);
				dsktpPaneMain.setBackground(new java.awt.Color(251,253,242));
				dsktpPaneMain.addContainerListener(new ContainerListener() {
					
					@Override
					public void componentRemoved(ContainerEvent e) {
						// TODO Auto-generated method stub
						//Made by Jon
						//teJinternalFrame[] temp = (teJinternalFrame[]) dsktpPaneMain.getAllFrames();
						killer((teJinternalFrame)e.getChild());
					}
					
					@Override
					public void componentAdded(ContainerEvent e) {
						// TODO Auto-generated method stub
						//By Jon
						launcher((teJinternalFrame)e.getChild());
					}
				});
				
			
			    
			
				pnlSideBar = new JPanel();	                               
				pnlSideBar.setBounds(0, 0, 176, 715);
				pnlSideBar.setBackground(new java.awt.Color(43,31,2));
				getContentPane().add(pnlSideBar);
				pnlSideBar.setLayout(null);
				
				createButtons();
				
				addComponentsToPane(pnlSideBar);
				

			pack();
			this.setSize(1436, 744);
		 }
	catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	
	
	}
	private ArrayList<teJinternalFrame> myWindows;
	
	public void launcher(teJinternalFrame newFrame)
	{//Jon
		boolean notFound = true;
		for(int i=0; i < myWindows.size()&&notFound; i++)
		{
			if(newFrame.getTitle()==myWindows.get(i).getTitle())
			{
				notFound=false;
				myWindows.get(i).setVisible(false);//Hopefully, this will hide, then show the window
				myWindows.get(i).setVisible(true);//forcing it to be on top
				newFrame.setVisible(false);
				//newFrame.dispose();
			}
		}
		if(notFound)
		{
			newFrame.setVisible(true);
			myWindows.add(newFrame);
		}
	}
	public void killer(teJinternalFrame deadFrame)
	{//Jon
		for(int i=0; i < myWindows.size(); i++)
			if(deadFrame.getTitle()==myWindows.get(i).getTitle())
				myWindows.remove(i);
	}
	private void killAll()
	{//Jon
		for(int i=0; i < myWindows.size(); i++)
		{	
			myWindows.get(i).setVisible(false);
			myWindows.get(i).dispose();
		}
		dsktpPaneMain.removeAll();
		dsktpPaneMain.repaint();
		myWindows=new ArrayList<teJinternalFrame>();
	}
	public void addWin(teJinternalFrame newWin)
	{//Jon
		dsktpPaneMain.add(newWin);
	}
}

